<template>
	<view class="container">
		<form @submit="submitForm" class="form">
			<view class="formBox">
				<page-head title="form"></page-head>
				<view class="uni-padding-wrap uni-common-mt">
					<form @submit="formSubmit" @reset="formReset">
						<!-- 既往病史 -->
						<view class="formItem1">
							<text><text class="bt">*</text>{{ app.language.rcjk_jwbs }}:</text>
							<view class="formB">
								<picker mode="selector" name="hjsx" :range="jwbsList" @change="onChangeJwbs">
									<view class="picker">
										{{selectedJwbsValue}}
									</view>
								</picker>
							</view>
						</view>
						<!-- 疾病名称 -->
						<view class="formItem1" v-if="isJwbs">
							<text><text class="bt">*</text>{{ app.language.rcjk_jbName }}:</text>
							<view class="formB">
								<input type="text" v-model="info.diseaseName"
									:placeholder="app.language.rcjk_IjbName" />
							</view>
						</view>
						<!-- 患病时间 -->
						<view class="formItem1" v-if="isJwbs">
							<text><text class="bt">*</text>{{ app.language.rcjk_hbTime }}:</text>
							<view class="formB">
								<picker mode="date" :value="hbtime" :start="startDate" :end="endDate"
									@change="bindHbTimeChange">
									<view class="uni-input">{{hbtime}}</view>
								</picker>
							</view>
						</view>
						<!-- 治疗情况 -->
						<view class="formItem1" v-if="isJwbs">
							<text><text class="bt">*</text>{{ app.language.rcjk_zlqk }}:</text>
							<view class="formB">
								<input type="text" v-model="info.treatmentStatus"
									:placeholder="app.language.rcjk_Izlqk" />
							</view>
						</view>
						<!-- 是否完成年度体检 -->
						<view class="formItem1">
							<text><text class="bt">*</text>{{ app.language.rcjk_isNdtj }}:</text>
							<view class="formB">
								<picker mode="selector" name="hjsx" :range="isNdtjList" @change="onChangeisNdtj">
									<view class="picker">
										{{selectedisNdtjValue}}
									</view>
								</picker>
							</view>
						</view>
						<!-- 体检时间 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_tjTime }}:</text>
							<view class="formB">
								<picker mode="date" :value="time" :start="startDate" :end="endDate"
									@change="bindDateChange">
									<view class="uni-input">{{time}}</view>
								</picker>
							</view>
						</view>
						<!-- 体检机构 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_tjJgName }}:</text>
							<view class="formB">
								<input type="text" v-model="info.inspectionInstitution"
									:placeholder="app.language.rcjk_ItjJgName" />
							</view>
						</view>
						<!-- 肺部 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_lungs }}:</text>
							<view class="formB">
								<input type="text" v-model="info.lungs" :placeholder="app.language.rcjk_Ilungs" />
							</view>
						</view>
						<!-- 胃部 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_stomach }}:</text>
							<view class="formB">
								<input type="text" v-model="info.stomach" :placeholder="app.language.rcjk_Istomach" />
							</view>
						</view>
						<!-- 肝功能 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_liverFunction }}:</text>
							<view class="formB">
								<input type="text" v-model="info.liverFunction"
									:placeholder="app.language.rcjk_IliverFunction" />
							</view>
						</view>
						<!-- 高压 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.jbjk_bloodPressure }}:</text>
							<view class="formB">
								<input type="text" v-model="info.bloodPressure"
									:placeholder="app.language.jbjk_IbloodPressure" />
							</view>
						</view>
						<!-- 低压 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.jbjk_bloodPressured }}:</text>
							<view class="formB">
								<input type="text" v-model="info.dycount"
									:placeholder="app.language.jbjk_IbloodPressured" />
							</view>
						</view>
						<!-- 心电图 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_electrocardiogram }}:</text>
							<view class="formB">
								<input type="text" v-model="info.electrocardiogram"
									:placeholder="app.language.rcjk_Ielectrocardiogram" />
							</view>
						</view>
						<!-- 左视力 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_leftVisualAcuity }}:</text>
							<view class="formB">
								<input type="text" v-model="info.leftVisualAcuity"
									:placeholder="app.language.rcjk_IleftVisualAcuity" />
							</view>
						</view>
						<!-- 右视力 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_rightVisualAcuity }}:</text>
							<view class="formB">
								<input type="text" v-model="info.rightVisualAcuity"
									:placeholder="app.language.rcjk_IrightVisualAcuity" />
							</view>
						</view>
						<!-- 尿常规 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_urinalysis }}:</text>
							<view class="formB">
								<input type="text" v-model="info.urinalysis"
									:placeholder="app.language.rcjk_Iurinalysis" />
							</view>
						</view>
						<!-- 血常规 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_bloodRoutine }}:</text>
							<view class="formB">
								<input type="text" v-model="info.bloodRoutine"
									:placeholder="app.language.rcjk_IbloodRoutine" />
							</view>
						</view>
						<!-- 健康情况 -->
						<view class="formItem1" v-if="isNdtj">
							<text><text class="bt">*</text>{{ app.language.rcjk_healthCondition }}:</text>
							<view class="formB">
								<input type="text" v-model="info.healthCondition"
									:placeholder="app.language.rcjk_IhealthCondition" />
							</view>
						</view>
						<!-- 提交 -->
						<view class="uni-btn-v">
							<button @click="submit" class="btn">提交修改</button>
							<!-- <button type="default" form-type="reset">Reset</button> -->
						</view>
					</form>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	import {
		addJbjk
	} from '@/api/user.js'
	const global = getApp().globalData;
	export default {
		data() {
			return {
				app: {
					global: global,
					name: global.appName,
					language: global.language
				},
				// 既往病史
				jwbsList: [],
				selectedJwbsValue: '',
				// 是否完成年检
				isNdtjList: [],
				selectedisNdtjValue: "",
				isNdtj: true,
				// 体检时间
				time: "",
				// 患病时间
				hbtime: "",
				token: "",
				hrUuid: "",
				info: {},
				isJwbs: false
			}
		},
		onLoad() {
			this.selectedisNdtjValue = this.app.language.qxz
			this.time = this.app.language.qxz
			this.hbtime = this.app.language.qxz
			this.selectedJwbsValue = this.app.language.qxz
			this.isNdtjList[0] = this.app.language.yes
			this.isNdtjList[1] = this.app.language.no

			this.jwbsList[0] = this.app.language.wu
			this.jwbsList[1] = this.app.language.xz
			this.jwbsList[2] = this.app.language.fb
			this.jwbsList[3] = this.app.language.gzjb
			this.jwbsList[4] = this.app.language.crbs
			this.jwbsList[5] = this.app.language.jzycbs
			this.jwbsList[6] = this.app.language.sss
			this.jwbsList[7] = this.app.language.scqk
			this.getToken()
		},
		methods: {
			// 获取token
			getToken() {
				let that = this
				uni.getStorage({
					key: 'loginData',
					success: function(res) {
						console.log('res', res)
						that.token = res.data.userInfo.appToken
						that.info = res.data.userInfo
					}
				});
			},

			// 既往病史
			onChangeJwbs(e) {
				console.log('e.detail.value', e.detail.value)
				this.selectedJwbsValue = this.jwbsList[e.detail.value];
				this.info.pastMedicalHistory = this.jwbsList[e.detail.value];
				if (e.detail.value == 0) {
					this.isJwbs = false
				} else {
					this.isJwbs = true
				}
			},

			// 是否完成年检
			onChangeisNdtj(e) {
				this.selectedisNdtjValue = this.isNdtjList[e.detail.value];
				this.info.completePhysicalExamination = this.isNdtjList[e.detail.value];
				if (e.detail.value == 0) {
					this.isNdtj = true
				} else {
					this.isNdtj = false
				}
			},

			// 体检时间选择
			bindDateChange(e) {
				this.time = e.detail.value
				this.info.inspectionTime = e.detail.value
			},

			// 患病时间
			bindHbTimeChange(e) {
				this.hbtime = e.detail.value
				this.info.illnessTime = e.detail.value
			},

			// 提交数据
			submit() {
				// 既往病史
				if (!this.selectedJwbsValue || this.selectedJwbsValue === this.app.language.qxz) {
					uni.showToast({
						title: this.app.language.rcjk_Ijwbs,
						icon: 'none',
					});
					return
				}
				if (this.isJwbs) {
					// 疾病名称
					if (!this.info.diseaseName) {
						uni.showToast({
							title: this.app.language.rcjk_IjbName,
							icon: 'none',
						});
						return
					}
					// 患病时间
					if (!this.hbtime || this.hbtime === this.app.language.qxz) {
						uni.showToast({
							title: this.app.language.rcjk_IhbTime,
							icon: 'none',
						});
						return
					}
					// 治疗情况
					if (!this.info.treatmentStatus) {
						uni.showToast({
							title: this.app.language.rcjk_Izlqk,
							icon: 'none',
						});
						return
					}
				}
				// 是否完成年度体检
				if (!this.selectedisNdtjValue || this.selectedisNdtjValue === this.app.language.qxz) {
					uni.showToast({
						title: this.app.language.rcjk_isNdtj,
						icon: 'none',
					});
					return
				}
				if (this.isNdtj) {
					// 体检时间
					if (!this.time || this.time === this.app.language.qxz) {
						uni.showToast({
							title: this.app.language.qxz + this.app.language.rcjk_tjTime,
							icon: 'none',
						});
						return
					}
					// 体检机构
					if (!this.info.inspectionInstitution) {
						uni.showToast({
							title: this.app.language.rcjk_ItjJgName,
							icon: 'none',
						});
						return
					}
					// 肺部
					if (!this.info.lungs) {
						uni.showToast({
							title: this.app.language.rcjk_Ilungs,
							icon: 'none',
						});
						return
					}
					// 胃部
					if (!this.info.stomach) {
						uni.showToast({
							title: this.app.language.rcjk_Istomach,
							icon: 'none',
						});
						return
					}
					// 肝功能
					if (!this.info.liverFunction) {
						uni.showToast({
							title: this.app.language.rcjk_IliverFunction,
							icon: 'none',
						});
						return
					}
					// 高压
					if (!this.info.bloodPressure) {
						uni.showToast({
							title: this.app.language.jbjk_IbloodPressure,
							icon: 'none',
						});
						return
					}
					// 低压
					if (!this.info.dycount) {
						uni.showToast({
							title: this.app.language.jbjk_IbloodPressured,
							icon: 'none',
						});
						return
					}
					// 心电图
					if (!this.info.electrocardiogram) {
						uni.showToast({
							title: this.app.language.rcjk_Ielectrocardiogram,
							icon: 'none',
						});
						return
					}
					// 左视力
					if (!this.info.leftVisualAcuity) {
						uni.showToast({
							title: this.app.language.rcjk_IleftVisualAcuity,
							icon: 'none',
						});
						return
					}
					// 右视力
					if (!this.info.rightVisualAcuity) {
						uni.showToast({
							title: this.app.language.rcjk_IrightVisualAcuity,
							icon: 'none',
						});
						return
					}
					// 尿常规
					if (!this.info.urinalysis) {
						uni.showToast({
							title: this.app.language.rcjk_Iurinalysis,
							icon: 'none',
						});
						return
					}
					// 血常规
					if (!this.info.bloodRoutine) {
						uni.showToast({
							title: this.app.language.rcjk_IbloodRoutine,
							icon: 'none',
						});
						return
					}
					// 健康情况
					if (!this.info.healthCondition) {
						uni.showToast({
							title: this.app.language.rcjk_IhealthCondition,
							icon: 'none',
						});
						return
					}
				}
				uni.showLoading({
					title: this.app.language.jzz,
				});
				this.info.hrUuid = this.info.uuid;
				this.info.department = this.info.department
				this.info.appToken = this.token
				console.log('通过了', this.info)
				addJbjk(this.info).then((res) => {
					if(res.code==200){
						uni.hideLoading();
						 uni.showToast({
							title: this.app.language.success,
							icon: 'success',
						 });
						 setTimeout(() => {
							console.log('跳转首页')
							uni.reLaunch({
								url: '/pages/user/jcHealth'
							 });
						 }, 1000); 
					}
				})
			}
		}
	}
</script>

<style>
	.container {
		padding: 20rpx;
	}

	.form {
		box-shadow: 1px 1px 10rpx #ddd;
		/* display: block; */
		width: 100%;
		padding-bottom: 20rpx;
	}

	.formItem {
		margin: 10rpx 0;
		padding: 10rpx;
		/* height: 162rpx; */
		position: relative;
		border-bottom: 1px dashed #ddd
	}

	.infoBox {
		border: 1px solid #ddd;
		padding: 0 20rpx;
		box-shadow: 1px 1px 10rpx #ddd;
	}

	.infoBox1 {
		border: 1px solid #ddd;
		margin-top: 20rpx;
		padding: 0 15rpx;
		box-shadow: 1px 1px 10rpx #ddd;
	}

	.formItem1 {
		margin: 10rpx 0;
		padding: 10rpx;
		/* height: 162rpx; */
		position: relative;
		border-bottom: 1px dashed #ddd
	}

	.formB {
		/* background-color: #f3f3f3; */
		font-size: 24rpx;
		padding: 10rpx;
		margin-top: 10rpx;
		position: absolute;
		right: 10rpx;
		top: -2rpx;
		text-align: right;
		color: #808080;
	}

	.formItem text,
	.formItem1 text {
		font-size: 26rpx;
	}

	.hjbImg button {
		font-size: 22rpx;
		width: 149rpx;
		height: 56rpx;
		background: #2f8ef9;
		color: #fff;
	}

	.hjbImg {
		/* position: absolute;
		bottom: 10rpx; */
		width: 150rpx;
		margin-top: 20rpx;
	}

	.scimage {
		width: 142rpx;
		height: 80rpx;
		position: absolute;
		bottom: 4rpx;
		left: 200rpx;
	}

	.title {
		margin-top: 30rpx;
		font-weight: bold;
		font-size: 28rpx;
	}

	.submit {
		background-color: #2f8ef9;
		color: #fff;
		font-size: 28rpx;
		width: 95%;
		margin: 20rpx auto 0;
	}

	.picker {
		padding: 10px 0;
		text-align: center;
		margin-top: -25rpx;
		text-align: right;
	}

	.bt {
		color: red;
		margin-right: 5rpx;
	}

	.btn {
		background-color: #2f8ef9;
		color: #fff;
		font-size: 28rpx;
		height: 60rpx;
		line-height: 60rpx;
		margin-top: 20rpx;
		width: 95%;
	}

	.uni-input {
		width: 262rpx;
		color: #808080;
		text-align: right !important;
	}

	.picker {
		width: 243rpx;
		color: #808080;
		text-align: right !important;
	}
</style>